package com.mind.core.net.handler;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mind.core.handler.MessageHandler;
import com.mind.core.net.msg.Message;

public  class HandlerManager {
	private static Logger logger = LoggerFactory.getLogger(HandlerManager.class);
	private static Map<Short,Class<? extends Message>> messages=new HashMap<Short,Class<? extends Message>>();
	private static Map<Short, MessageHandler> handlers=new HashMap<Short, MessageHandler>();
	private final static DumyHandler dumyHandler = new DumyHandler();
	public static Message createMessage(Short type) {
		Class base = messages.get(type);
		if(base==null){
			logger.error("can not find message type="+type);
			return null;
		}
		try {
			return (Message) base.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void register(Class<? extends Message> messageClass) {
		if (messageClass == null) {
			throw new IllegalArgumentException("No messageClass");
		}
		Class _class = null;
		try {
			short type=messageClass.newInstance().getMessageType();
			_class = messages.get(type);
			if (logger.isDebugEnabled()) {
				logger.debug("Add message for type [" + type + "],message class " + messageClass);
			}
			if (_class != null) {
				logger.warn("Duplicate Message type for type [" + type + " class1="+_class+" class2="+messageClass+"]");
			}
			messages.put(type, messageClass);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	public static MessageHandler getHandler(short type) {
		MessageHandler handler = handlers.get(type);
		if (handler == null) {
			return dumyHandler;
		}
		return handler;
	}
	/**检查加载的message和handler是否正确
	 * @return
	 */
	public static boolean check(){
		if(handlers.size()!=messages.size()){
			return false;
		}
		for(short type:handlers.keySet()){
			if(messages.get(type)==null){
				return false;
			}
		}
		return true;
	}
	
	
	/**
	 * 注册IMessageHandler
	 * 
	 * @param handler
	 * @throws IllegalStateException
	 *             当重复注册同一消息的类型的处理器时会抛出此异常
	 * @throws IllegalArgumentException
	 *             handler为空时
	 * 
	 */
	public static void register(MessageHandler handler) {
		if (handler == null) {
			throw new IllegalArgumentException("No handler");
		}
		if (handler.getTypes() == null) {
			logger.warn(handler.getClass().getName()+" has no types");
			return;
		}
		for(short type:handler.getTypes()){
			MessageHandler _handler = handlers.get(type);
			if (_handler != null) {
				logger.warn("Duplicate Message Handler for type [" + type + "]");
			}
			handlers.put(type, handler);
		}
	}
	public static void execute(Message message) {
		getHandler(message.getMessageType()).execute(message);
	}
}
